package netty.cli;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
	public static void main(String[] args) throws InterruptedException {
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel socketChannel) throws Exception {
				socketChannel.pipeline().addLast(new ClientHandler());
			}
		});
		ChannelFuture future = bootstrap.connect("127.0.0.1", 8990).sync();
		future.channel().writeAndFlush(Unpooled.copiedBuffer("我是客户端".getBytes()));
		future.channel().closeFuture().sync();
		
		// 关闭EventLoopGroup
		workerGroup.shutdownGracefully();
	}
}
